//
//  AppEntrance.m
//  Fantasia
//
//  Created by kongkong on 16/9/17.
//  Copyright © 2016年 Kongkong. All rights reserved.
//

#import "AppEntrance.h"
#import "RootVC.h"
#import "HomeViewcontroller.h"
#import "LoginViewController.h"

@interface AppEntrance ()
@property(nonatomic, strong) UIWindow *windows;
@property(nonatomic, strong) NSOperationQueue *startQueue;
@end

@implementation AppEntrance

+ (AppEntrance *)instance {
    static AppEntrance *_instance = nil;

    @synchronized (self) {
        if (_instance == nil) {
            _instance = [[self alloc] init];
        }
    }

    return _instance;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    RootVC *root = [[RootVC alloc] initWithRootViewController:[[HomeViewcontroller alloc] init]];

    [[FantasiaStorage instance] setMemoryValue:root forKey:GlobalNavigationControllerKey];

    self.windows = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.windows.rootViewController = root;

    [self.windows makeKeyAndVisible];

    [self loadTask];

    return YES;
}

#pragma -mark Queue for start tasks
- (NSOperationQueue *)startQueue {
    static NSOperationQueue* queue = nil;
    @synchronized (self) {
        if (!queue) {
            queue = [NSOperationQueue mainQueue];
        }
    }
    return queue;
}

- (void)loadTask {
    [self.startQueue addOperation:[self appearanceTask]];
    [self.startQueue addOperation:[self requestHeadTask]];
    [self.startQueue addOperation:[self splashTask]];
    [self.startQueue addOperation:[self loginTask]];
}

- (NSOperation *)splashTask {
    return [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"start Queue");
    }];
//    return nil;
}

- (NSOperation *)requestHeadTask{
    return [NSBlockOperation blockOperationWithBlock:^{
        [[KKRequestEngine instance] setCustomerHead:[NSString udid] forKey:@"kk_udid"];
    }];
}

- (NSOperation *)loginTask {
    return [NSBlockOperation blockOperationWithBlock:^{
        LoginViewController *login = [[LoginViewController alloc] init];
        [[self.windows.rootViewController rootNavigationController] pushViewController:login animated:NO];
    }];
}


- (NSOperation *)appearanceTask{
    return [NSBlockOperation blockOperationWithBlock:^{
        [UINavigationBar appearance].tintColor = [UIColor orangeColor];
    }];
    
}
@end
